package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.admin.dtos.SensitiveDto;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Transactional
@Service
@RequiredArgsConstructor
@Slf4j
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService {

    /**
     * 删除
     * @param id
     * @return
     */
    @Override
    public ResponseResult del(Integer id) {
        if(id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 查询
     * @param sensitiveDto
     * @return
     */
    @Override
    public ResponseResult findlist(SensitiveDto sensitiveDto) {
        //校验参数
        sensitiveDto.checkParam();

        //分页查询
        IPage page=new Page(sensitiveDto.getPage(),sensitiveDto.getSize());
        LambdaQueryWrapper<WmSensitive> lambdaQueryWrapper=new LambdaQueryWrapper<>();

        //创建时间倒序
        lambdaQueryWrapper.orderByDesc(WmSensitive::getCreatedTime);

        //按照敏感词模糊查询
        if(StringUtils.isNotBlank(sensitiveDto.getName())){
            lambdaQueryWrapper.like(WmSensitive::getSensitives,sensitiveDto.getName());
        }

        page=page(page,lambdaQueryWrapper);

        ResponseResult result=new PageResponseResult(sensitiveDto.getPage(),sensitiveDto.getSize(), (int) page.getTotal());
        result.setData(page.getRecords());
        return result;
    }

    /**
     * 新增
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult saveSensitive(AdSensitive adSensitive) {
        if(adSensitive==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        //判断是否存在
        WmSensitive sensitive = getOne(Wrappers.<WmSensitive>lambdaQuery().eq(WmSensitive::getSensitives, adSensitive.getSensitives()));
        if(sensitive!=null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        //不存在，保存
        WmSensitive wmSensitive=new WmSensitive();
        BeanUtils.copyProperties(adSensitive,wmSensitive);
        wmSensitive.setCreatedTime(new Date());
        save(wmSensitive);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 修改
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult updateSensitive(AdSensitive adSensitive) {
        if(adSensitive==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        //更新
        WmSensitive wmSensitive=new WmSensitive();
        BeanUtils.copyProperties(adSensitive,wmSensitive);
        wmSensitive.setCreatedTime(new Date());
        updateById(wmSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


}
